<template>
	<div class="wrapper">
		<div class="header">加好友</div>
		<div class="content">
			<div class="search">
				<div class="text">
					<input type="text" name="search" placeholder="搜索好友账号或昵称">
				</div>
				<span class="search_btn" @click.stop="searchFriend(loginInfo)"><img src="img/search_btn.png"></span>
			</div>
			<div class="list_wrapper" v-if="userInfo">
				<div class="list_item2">
					<div class="item2_inner_right">
						<div class="list_item2_left">
							<img :src="userInfo.Tag_Profile_IM_Image?userInfo.Tag_Profile_IM_Image:''" @click="readDetail(userInfo.To_Account)">
						</div>
						<div class="list_item2_right">
							<span class="friend_name" v-if="userInfo.Tag_Profile_IM_Nick" :style="{'color':(userInfo.Tag_Profile_IM_Gender=='Gender_Type_Female')?'#F29BBA':'#0096FF'}">{{userInfo.Tag_Profile_IM_Nick}}</span>
							<span class="friend_desc">{{'账号:'+userInfo.To_Account}}</span>
						</div>
					</div>
					<div class="item2_inner_left" id="add_friend" @click="toAdd()">加好友</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
import $ from "jquery";
import Client from "../../config/common";
export default {
  data() {
    return {
      loginInfo: {},
      userInfo: null
    };
  },
  created() {
    Client.login(loginInfo => {
      this.loginInfo = loginInfo;
    });
  },
  mounted() {},

  components: {
    // headTop
  },

  computed: {},

  methods: {
    searchFriend(loginInfo) {
      let tag_list = [
        "Tag_Profile_IM_Nick", //昵称
        "Tag_Profile_IM_Gender", //性别
        "Tag_Profile_IM_AllowType", //加好友方式
        "Tag_Profile_IM_Image" //头像
      ];
      let inputVal = $("input[name=search]").val();
      let objs = {
        To_Account: [inputVal],
        TagList: tag_list
      };
      //搜索用户
      webim.getProfilePortrait(
        objs,
        resp => {
          if (resp.UserProfileItem && resp.UserProfileItem.length > 0) {
            let userInfos = resp.UserProfileItem[0];
            if (userInfos.ProfileItem) {
                let sonItem = userInfos.ProfileItem;
                sonItem.forEach((ele, index) => {
                userInfos[ele.Tag] = ele.Value;
              });
              this.userInfo = userInfos;
              Client.log(this.userInfo);
            }
          }
          /** */
        },
        function(err) {
          alert(err.ErrorInfo);
        }
      );
    },
    toAdd() {
	  const allowType = this.userInfo.Tag_Profile_IM_AllowType;
      //判断对方是否允许加好友
      if (allowType == "AllowType_Type_NeedConfirm") {
        //需要验证
		    const theNick = encodeURI(this.userInfo.nick);
        //userInfo.nick = encodeURI(userInfo.nick);
		    const img = this.userInfo.Tag_Profile_IM_Image,
		      id = this.userInfo.To_Account,
			  nick = this.userInfo.Tag_Profile_IM_Nick;
		    this.$router.push({path:'/friendCheck', query:{id,img,nick}});
      } else if (allowType == "AllowType_Type_DenyAny") {
        //拒绝任何人加加好友
        alert("该用户拒绝任何人添加好友");
      } else {
        //允许任何人
        //申请加好友
        var applyAddFriend = (() => {
          var add_friend_item = [
            {
              To_Account: $("input[name=search]").val(),
              AddSource: "AddSource_Type_Unknow",
              AddWording: "" //加好友附言，可为空
            }
          ];
          var options = {
            From_Account: this.loginInfo.identifier,
            AddFriendItem: add_friend_item
          };

          webim.applyAddFriend(
            options,
            resp => {
              console.log(resp);
              if (resp.Fail_Account && resp.Fail_Account.length > 0) {
                for (var i in resp.ResultItem) {
                  alert(resp.ResultItem[i].ResultInfo);
                  break;
                }
              } else {
                alert("添加好友成功");
              }
            },
            function(err) {
              alert(err.ErrorInfo);
            }
          );
        })();
      }
    },
    //跳转好友详情
    readDetail(account){
      this.$router.push({path: "friendDetail", query:{id: account}});
    }
  }
};
</script>

<style lang="scss">
@import "../../style/add_friend";
</style>